﻿using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using Person=StockholmsStad.Data.Entity.Person;

namespace StockholmsStad.Data.ContractFactory
{
    /// <summary>
    /// Konvertering befintligt dataobjekt till kontraktsobjekt
    /// </summary>
    internal class PersonFactory
    {
        private const string SWEDISH_MEMBERSHIP = "Svenskt";

        public Person CreateEntityObject(ContactPerson contactPerson)
        {
            if (contactPerson == null)
                return null;
            var entityPerson = new Person
                                   {
                                       FirstNames = contactPerson.FirstNames,
                                       LastName = contactPerson.LastName
                                   };

            if (!string.IsNullOrEmpty(contactPerson.Email))
                entityPerson.EmailAddress = contactPerson.Email;

            if (!string.IsNullOrEmpty(contactPerson.Telephone))
                entityPerson.PhoneNumber = contactPerson.Telephone;

            return entityPerson;
        }

        public ContactPerson CreateContractContact(Person person)
        {
            if (person == null)
                return null;
            var contractPerson = new ContactPerson
                                     {
                                         FirstNames = person.FirstNames,
                                         LastName = person.LastName,
                                     };

            if (!string.IsNullOrEmpty(person.EmailAddress))
                contractPerson.Email = person.EmailAddress;

            if (!string.IsNullOrEmpty(person.PhoneNumber))
                contractPerson.Telephone = person.PhoneNumber;

            return contractPerson;
        }

        public Person CreateEntityObject(Runtime.Serialization.DataContract.Person person)
        {
            var factory = new AddressFactory();

            var entityPerson = new Person
                                   {
                                       FirstNames = person.FirstNames,
                                       LastName = person.LastName,
                                       PersonId = person.PersonId
                                   };

            if (person.Address != null)
            {
                entityPerson.Address = factory.CreateEntityObject(person.Address);
            }

            if (!string.IsNullOrEmpty(person.Nationality))
                entityPerson.Nationality = person.Nationality;

            if (person.SwedishNationality)
                entityPerson.Nationality = SWEDISH_MEMBERSHIP;

            if(person.Sex.HasValue)
                entityPerson.SexMale = person.Sex.Value == Sex.Male ? true : false;

            entityPerson.ProtectedIdentity = person.ProtectedIdentity;

            if (!string.IsNullOrEmpty(person.PhoneNumber))
                entityPerson.PhoneNumber = person.PhoneNumber;

            if (!string.IsNullOrEmpty(person.Email))
                entityPerson.EmailAddress = person.Email;

            if (!string.IsNullOrEmpty(person.PersonalNumber))
                entityPerson.PersonalNumber = person.PersonalNumber;

            return entityPerson;
        }

        public Runtime.Serialization.DataContract.Person CreateContractObject(Person person)
        {
            var factory = new AddressFactory();

            var contractPerson = new Runtime.Serialization.DataContract.Person
                                     {
                                         FirstNames = person.FirstNames,
                                         LastName = person.LastName,
                                         PersonId = person.PersonId
                                     };

            if (person.Address != null)
            {
                contractPerson.Address = factory.CreateContractObject(person.Address);
            }

            if (!string.IsNullOrEmpty(person.Nationality))
            {
                // Inte svensk
                contractPerson.Nationality = person.Nationality;
                contractPerson.SwedishNationality = person.Nationality.Equals(SWEDISH_MEMBERSHIP) ? true : false;
            }

            if(person.SexMale.HasValue)
                contractPerson.Sex = person.SexMale.Value ? Sex.Male : Sex.Female;

            contractPerson.ProtectedIdentity = person.ProtectedIdentity.HasValue
                                                   ? person.ProtectedIdentity.Value
                                                   : false;

            if (!string.IsNullOrEmpty(person.PhoneNumber))
                contractPerson.PhoneNumber = person.PhoneNumber;

            if (!string.IsNullOrEmpty(person.EmailAddress))
                contractPerson.Email = person.EmailAddress;

            if (!string.IsNullOrEmpty(person.PersonalNumber))
                contractPerson.PersonalNumber = person.PersonalNumber;

            if (!string.IsNullOrEmpty(person.CellPhoneNumber))
                contractPerson.CellPhoneNumber = person.CellPhoneNumber;

            return contractPerson;
        }

        public void UpdateEntityObject(ContactPerson contactPerson, Person entityPerson)
        {
            entityPerson.FirstNames = contactPerson.FirstNames;
            entityPerson.LastName = contactPerson.LastName;
            entityPerson.EmailAddress = contactPerson.Email;
            entityPerson.PhoneNumber = contactPerson.Telephone;
        }

        public void UpdateEntityObject(Runtime.Serialization.DataContract.Person person, Person entityPerson)
        {
            AddressFactory factory = FactoryAgent.GetAddressFactory();
            if (person.Address == null)
            {
                entityPerson.Address = null;
            }
            else if (entityPerson.Address == null)
            {
                entityPerson.Address = factory.CreateEntityObject(person.Address);
            }
            else
            {
                factory.UpdateEntityObject(person.Address, entityPerson.Address);
            }
            entityPerson.FirstNames = person.FirstNames;
            entityPerson.LastName = person.LastName;
            entityPerson.EmailAddress = person.Email;
            entityPerson.PhoneNumber = person.PhoneNumber;
            entityPerson.CellPhoneNumber = person.CellPhoneNumber;
            entityPerson.Nationality = person.SwedishNationality ? SWEDISH_MEMBERSHIP : person.Nationality;
            entityPerson.SexMale = person.Sex == Sex.Male ? true : false;
            entityPerson.ProtectedIdentity = person.ProtectedIdentity;
            entityPerson.PersonalNumber = person.PersonalNumber;
        }

        public void UpdateEntityObject(Minister minister, Person entityPerson)
        {
            AddressFactory factory = FactoryAgent.GetAddressFactory();
            if (minister.Address == null)
            {
                entityPerson.Address = null;
            }
            else if (entityPerson.Address == null)
            {
                entityPerson.Address = factory.CreateEntityObject(minister.Address);
            }
            else
            {
                factory.UpdateEntityObject(minister.Address, entityPerson.Address);
            }
            entityPerson.FirstNames = minister.FirstNames;
            entityPerson.LastName = minister.LastName;
            entityPerson.EmailAddress = minister.Email;
            entityPerson.PhoneNumber = minister.PhoneNumber;
            entityPerson.CellPhoneNumber = minister.CellPhoneNumber;
        }
    }
}